/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package tools;

import com.openbrew.openbrewrf.BarrelConditioning;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

/**
 *
 * @author chapmand
 */
public class BarrelConditioningTools {
    /**
     * Updates the tank volume when a bottle movement is done.
     * @param en - Entity Manager
     * @param volume - Volume of beer being moved.
     * @param bcsID - ID of the barrel conditioning brew being modified.
     */
    public void updateTankVolumeForMovement(final EntityManager en
            , final double volume, final int bcsID){
        BarrelConditioning updatedBatch = getBarrelConditioningFromID(en, bcsID);
        double newVolume = updatedBatch.getBCVolLeft() - volume;
        if(newVolume < 0) {
            newVolume=0;
        }
        updatedBatch.setBCVolLeft((float)newVolume);
        final java.util.Date date = new java.util.Date();
        updatedBatch.setBCDatePkg(date);
        en.merge(updatedBatch);
    }
    
    private BarrelConditioning getBarrelConditioningFromID(final EntityManager en, final int bcsID){
        TypedQuery<BarrelConditioning> query = en.createQuery(
                "SELECT b FROM BarrelConditioning b WHERE b.bCId = ?1", BarrelConditioning.class);
        query.setParameter(1, bcsID);
        BarrelConditioning updatedBatch = query.getSingleResult();
        return updatedBatch;
    }
}
